package com.atguigu.vodservice.controller;

import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.ResultCode;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.atguigu.vodservice.serivce.VideoService;
import com.atguigu.vodservice.util.AliyunVodSDKUtils;
import com.atguigu.vodservice.util.VideoProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @title: ConstantPropertiesUtil
 * @Author danghailiang
 * @Date: 2022/9/30 0:56
 * @Version 1.0
 */
@Api(description = "视频点播服务")
@RestController
@RequestMapping("/api/vod")
public class VodController {

    @Resource
    private VideoService videoService;

    @ApiOperation("视频上传")
    @PostMapping("/uploadAlyVideo")
    public R uploadAlyVideo(
            @ApiParam(value = "视频", required = true)
                    MultipartFile file) throws IOException {
        String videoId = videoService.uploadAlyVideo(file);
        return R.ok().data(videoId);
    }

    @ApiOperation("视频删除")
    @DeleteMapping("/delAlyVideoById/{videoSourceId}")
    public R delAlyVideoById(
            @ApiParam(value = "视频", required = true)
            @PathVariable("videoSourceId") String videoSourceId) {
        videoService.delAlyVideoById(videoSourceId);
        return R.ok();
    }
    @ApiOperation("视频批量删除")
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestParam("ids") @RequestBody List<String> ids) {
        videoService.deleteBatch(ids);
        return R.ok();
    }

    @ApiOperation("获取视屏凭证")
    @GetMapping("/getVideoAuth/{id}")
    public R getVideoAuth(
            @PathVariable("id") String videoId
    ) throws ClientException {
        //初始化客户端、请求对象和相应对象
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(VideoProperties.ACCESS_KEY_ID, VideoProperties.ACCESS_KEY_SECRET);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = null;
        try {
            //设置请求参数
            request.setVideoId(videoId);
            //获取请求响应
            response = client.getAcsResponse(request);
            System.out.println("response.getPlayAuth()："+response.getPlayAuth());
            //播放凭证
            return R.ok().data(response.getPlayAuth());
        } catch (Exception e) {
            throw  new GuLiException(ResultCode.ERROR,e.getMessage());
        }
    }
}
